package com.github.taomus.gitoys.config

import com.github.taomus.gitoys.config.entity.Config
import java.io.FileReader
import org.yaml.snakeyaml.Yaml
import java.io.FileWriter

class ConfigManager {
	var static String configPath = "./config.yml"

	def static read() {
		var url = Thread.currentThread().getContextClassLoader().getResource(configPath);
		if (url !== null) {
			configPath = url.getPath();
		}
		return new Yaml().loadAs(new FileReader(configPath), Config);
	}
	
	/**
	 * 写配置文件
	 */
	def static write(Config config) {
		var url = Thread.currentThread().getContextClassLoader().getResource(configPath);
		if (url !== null) {
			configPath = url.getPath();
		}
		new Yaml().dump(config, new FileWriter(configPath));
	}
}
